package sz.szsmk.citizencard.ui.activity;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.chenenyu.router.annotation.Route;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SpecialActivityBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.mvp.contract.ISprcialContact;
import sz.szsmk.citizencard.mvp.presenter.SpecialPresenter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.util.TimeCount;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/10 17:56
 * description:专有账户修改密码
 */
@Route("specialpsd")
public class SpecialCardPsdActivity extends IBaseActivity<SpecialPresenter, SpecialActivityBinding>
        implements ISprcialContact.ISprcialView, View.OnClickListener {

    private String cardNo;
    private String psdNew;
    private String newPsd;
    private String oldPsd;
    private String cardNo1;
    private String userPhone;
    private String phone;
    private TimeCount timeCount;
    private String smsCode;

    @Override
    protected int attachLayoutRes() {
        return R.layout.special_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);

    }

    @Override
    protected void initViews() {
        dataBinding.titleLayout.settingTitle.setText("修改专有账户密码");
        dataBinding.sprcialBtn.setOnClickListener(this);
        cardNo1 = getIntent().getStringExtra("cardNo");
        dataBinding.specialCard.setEnabled(false);
        dataBinding.specialCard.setFocusable(false);
        dataBinding.specialCard.setText(cardNo1);
        dataBinding.specialGetcodeBtn.setOnClickListener(this);


    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void modifyPsdSucc(String message) {
        final SuccessDialog dialog = new SuccessDialog(this, "操作成功");
        dialog.show();

        new Handler().postDelayed(() -> {
            dialog.cancel();
            finish();
        }, 2000);
    }

    @Override
    public void modifyPsdFail(String message) {
        PubUtils.popTipOrWarn(this, message);
    }

    @Override
    public void sprcialGetCodeSucc() {

    }

    private void setTimeCount() {
        timeCount = new TimeCount(dataBinding.specialGetcodeBtn, 60000, 1000);
        timeCount.start();
    }

    @Override
    public void sprcialGetCodeFail() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.sprcial_btn) {
            cardNo = dataBinding.specialCard.getText().toString();
            psdNew = dataBinding.specialNew.getText().toString();
            phone = dataBinding.specialPhone.getText().toString().trim();
            newPsd = dataBinding.specialNewpsd.getText().toString();
            oldPsd = dataBinding.specialOldpsd.getText().toString();
            smsCode = dataBinding.specialCode.getText().toString().trim();
            if (TextUtils.isEmpty(cardNo)) {
                PubUtils.popTipOrWarn(this, "卡号为空");
                return;
            }
            if (TextUtils.isEmpty(phone)) {
                PubUtils.popTipOrWarn(this, "请输入专有账户预留手机号");
                return;
            }
            if (TextUtils.isEmpty(oldPsd)) {
                PubUtils.popTipOrWarn(this, "原密码为空");
                return;
            }
            if (oldPsd.length() != 6) {
                PubUtils.popTipOrWarn(this, "请输入六位数的密码");
                return;
            }
            if (TextUtils.isEmpty(psdNew)) {
                PubUtils.popTipOrWarn(this, "新密码为空");
                return;
            }


            if (TextUtils.isEmpty(newPsd)) {
                PubUtils.popTipOrWarn(this, "请再次输入新密码");
                return;
            }

            if (psdNew.length() != 6 || newPsd.length() != 6) {
                PubUtils.popTipOrWarn(this, "请输入六位数的密码");
                return;
            }
//                mPresenter.modifyPsd(cardNo,oldPsd,psdNew,newPsd);
            mPresenter.modifyPsd(phone, cardNo, oldPsd, psdNew, smsCode);
        } else if (id == R.id.special_getcode_btn) {
            userPhone = dataBinding.specialPhone.getText().toString().trim();
            if (TextUtils.isEmpty(userPhone)) {
                PubUtils.popTipOrWarn(this, "请输入专有账户预留手机号");
                return;
            }
            setTimeCount();
            mPresenter.sprcialGetCode(userPhone);
//                timeCount.start();
        } else if (id == R.id.back) {
            finish();
        }
    }
}
